package com.pdsu.graduation_design.views;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.pdsu.graduation_design.MainActivity;
import com.pdsu.graduation_design.R;
import com.pdsu.graduation_design.dto.CommodityDTO;
import com.pdsu.graduation_design.dto.PayCommodityDTO;
import com.pdsu.graduation_design.utils.JsonUtil;
import com.pdsu.graduation_design.utils.PicassoImageLoader;
import com.pdsu.graduation_design.utils.Result;
import com.squareup.picasso.Picasso;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class MainListViewItemDetailActivity extends AppCompatActivity {

    //
    private ImageView detail_like_imv;
    private Button buy_now_btn,add_car_btn;
    private int cid_temp;


    //数据接受
    private TextView cname,cprice,cnumber,u_miaoshu,u_sudu,u_taidu,commodity_detail_store_title_tv,commodity_detail_cmessage_tv;
    private ImageView cimage;

    /**
     * 接收商品id  在请求后台接口  获取商品信息填充到UI中
     * @param savedInstanceState
     */
    private int cid;
    private Banner banner;
    private List<String> images;
    private List<String> titles;
    private Context context;
    private String image_url;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_list_view_item_detail);
        init();
        isLike();
        operation();
    }

    private void init() {

        detail_like_imv=findViewById(R.id.detail_like_imv);
        buy_now_btn=findViewById(R.id.detail_buynow_btn);
        add_car_btn=findViewById(R.id.detail_addTocar_btn);

        context=this;

//        private TextView cname,cprice,cnumber,u_miaoshu,u_sudu,u_taidu;
//        private ImageView cimage;

        commodity_detail_cmessage_tv=findViewById(R.id.commodity_detail_cmessage_tv);
        cname=findViewById(R.id.commodity_detail_title_tv);
        cprice=findViewById(R.id.commodity_detail_price_tv);
        u_miaoshu=findViewById(R.id.commodity_detail_miaoshu_tv);
        u_sudu=findViewById(R.id.commodity_detail_sudu_tv);
        u_taidu=findViewById(R.id.commodity_detail_taidu_tv);
        cimage=findViewById(R.id.commodity_detail_image_imv);
        cnumber=findViewById(R.id.commodity_detail_cnumber_tv);
        commodity_detail_store_title_tv=findViewById(R.id.commodity_detail_store_title_tv);

        cid= getIntent().getIntExtra("cid",-1);
        images=new ArrayList<>();
        titles=new ArrayList<>();
        banner = findViewById(R.id.main_listview_detail_banner);

        getData();
    }

    void getData(){
        RequestParams requestParams=new RequestParams(MainActivity.URL+"/commodity/getCommodityStoreByCID");
        requestParams.addParameter("cid",cid);
        x.http().post(requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                try {
                    Result r= JsonUtil.JsonToObj(result,Result.class);
                    if (r.getCode().equals("200")){
                        CommodityDTO commodityDTO=JsonUtil.JsonToObj(r.getData().toString(),CommodityDTO.class);

                        bind(commodityDTO);



                    }

                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void bind(CommodityDTO commodityDTO) {
        System.out.println(commodityDTO.toString());
        cid_temp=commodityDTO.getCid();
        cname.setText(commodityDTO.getCname());
        cprice.setText(commodityDTO.getPrice()+"");
        u_miaoshu.setText(commodityDTO.getUser_miaoshu()+"");
        u_taidu.setText(commodityDTO.getUser_taidu()+"");
        u_sudu.setText(commodityDTO.getUser_sudu()+"");
        commodity_detail_store_title_tv.setText(commodityDTO.getUsername());
        cnumber.setText(commodityDTO.getCnumber()+"");
        commodity_detail_cmessage_tv.setText(commodityDTO.getCmessage());
        image_url=commodityDTO.getImage();
        Picasso.with(context).load(commodityDTO.getImage()).into(cimage);

        System.out.println("-----------bind");
        System.out.println(commodityDTO.toString());
        images.add(commodityDTO.getImage());
        titles.add(commodityDTO.getCname());
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE);
        //picasso加载图片
        banner.setImageLoader(new PicassoImageLoader());
        banner.setBannerAnimation(Transformer.Accordion);
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                Toast.makeText(getApplicationContext(), "click " + position, Toast.LENGTH_SHORT).show();
            }
        });
        banner.isAutoPlay(true);
        banner.setDelayTime(1500);
        banner.setImages(images);
        banner.setBannerTitles(titles);
        banner.start();




    }

    private void operation(){

        detail_like_imv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println(detail_like_imv.getImageAlpha());

                RequestParams requestParams =new RequestParams();
                requestParams.addParameter("uid",MainActivity.user_admin.getUid());
                requestParams.addParameter("cid",cid);
                if (detail_like_imv.getImageAlpha()==255){
                    requestParams.setUri(MainActivity.URL+"/commodity/addLikeCommodity");
                    x.http().post(requestParams, new Callback.CommonCallback<String>() {
                        @Override
                        public void onSuccess(String result) {

                            try {
                                Result r= JsonUtil.JsonToObj(result,Result.class);
                                if (r.getCode().equals("200")){
                                    Toast.makeText(context, "收藏成功", Toast.LENGTH_SHORT).show();
                                    detail_like_imv.setImageResource(R.drawable.like_true);
                                    detail_like_imv.setImageAlpha(254);
                                }
                            } catch (JsonProcessingException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onError(Throwable ex, boolean isOnCallback) {

                        }

                        @Override
                        public void onCancelled(CancelledException cex) {

                        }

                        @Override
                        public void onFinished() {

                        }
                    });


                }else {
                    requestParams.setUri(MainActivity.URL+"/commodity/delLikeCommodity");
                    x.http().post(requestParams, new Callback.CommonCallback<String>() {
                        @Override
                        public void onSuccess(String result) {

                            try {
                                Result r= JsonUtil.JsonToObj(result,Result.class);
                                if (r.getCode().equals("200")){
                                    Toast.makeText(context, "取消收藏", Toast.LENGTH_SHORT).show();
                                    detail_like_imv.setImageResource(R.drawable.like_false);
                                    detail_like_imv.setImageAlpha(255);
                                }
                            } catch (JsonProcessingException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onError(Throwable ex, boolean isOnCallback) {

                        }

                        @Override
                        public void onCancelled(CancelledException cex) {

                        }

                        @Override
                        public void onFinished() {

                        }
                    });


                }


            }
        });

        add_car_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                //
                RequestParams requestParams=new RequestParams(MainActivity.URL+"/commodity/addCommodityToCar");
                requestParams.addParameter("uid",MainActivity.user_admin.getUid());
                requestParams.addParameter("cid",cid);
                x.http().post(requestParams, new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String result) {

                        Result res3= null;
                        try {
                            res3 = JsonUtil.JsonToObj(result, Result.class);
                            if (res3.getCode().equals("200")){

                                Toast.makeText(context, "添加成功", Toast.LENGTH_SHORT).show();

                                MainActivity.initUser();
//                                List<PayCommodityDTO> commodityDTOS= JsonUtil.json2list(res3.getData().toString(),PayCommodityDTO.class);
                                //Lis


                            }else {
                                Toast.makeText(context, "购物车已存在该商品", Toast.LENGTH_SHORT).show();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {

                    }

                    @Override
                    public void onCancelled(CancelledException cex) {

                    }

                    @Override
                    public void onFinished() {

                    }
                });

            }
        });

        buy_now_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                 List<PayCommodityDTO> payCommodityDTOS_to_order=new ArrayList<>();
                 PayCommodityDTO payCommodityDTO=new PayCommodityDTO();
                 payCommodityDTO.setUcnumber(1);
                 payCommodityDTO.setCid(cid_temp);
                 payCommodityDTO.setCname(cname.getText().toString());
                 payCommodityDTO.setPrice(Double.valueOf(cprice.getText().toString()));
                 payCommodityDTO.setImage(image_url);
                 payCommodityDTO.setUsername(commodity_detail_store_title_tv.getText().toString());
                 payCommodityDTOS_to_order.add(payCommodityDTO);
                try {

                    Intent intent=new Intent(context,PayOrderActivity.class);
                    String json_data = JsonUtil.ListToJson(payCommodityDTOS_to_order);
                    intent.putExtra("json_data_pa",json_data);
                    intent.putExtra("type_http",0);
                    intent.putExtra("all_price",cprice.getText().toString());
                    startActivity(intent);
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }


            }
        });

    }

    private void isLike(){
        RequestParams requestParams=new RequestParams(MainActivity.URL+"/commodity/getCommodityStoreByCID");
        requestParams.addParameter("cid",cid);

        requestParams.addParameter("uid",MainActivity.user_admin.getUid());
        requestParams.setUri(MainActivity.URL+"/commodity/commodityIsLike");
        x.http().post(requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    Result res=JsonUtil.JsonToObj(result,Result.class);
                    if (res.getCode().equals("200")){
                        detail_like_imv.setImageAlpha(254);
                        detail_like_imv.setImageResource(R.drawable.like_true);
                    }
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }


}